#ifndef __HASH_TABLE_H__
#define __HASH_TABLE_H__

#include "sys.h"

/* HashTable */
struct HashTable
{
    struct kv ** table;
    int table_size;
};
typedef struct HashTable HashTable;

#ifdef __cplusplus
extern "C" {
#endif

//------------------------------------key=char
/* new a HashTable instance */
HashTable* hash_table_new_str(int tableSize);

/* delete a HashTable instance */
void hash_table_delete_str(HashTable* ht);

/* delete a HashTable instance */
void hash_table_delete_str(HashTable* ht);

/* insert or update a value indexed by key */
int hash_table_put2_str(HashTable* ht, char* key, void* value, void(*free_value)(void*));

/* get a value indexed by key */
void* hash_table_get_str(HashTable* ht, char* key);

/* remove a value indexed by key */
void hash_table_rm_str(HashTable* ht, char* key);

//------------------------------------key=int
/* new an instance of HashTable */
HashTable* hash_table_new(int tableSize);

/*
delete an instance of HashTable,
all values are removed auotmatically.
*/
void hash_table_delete(HashTable* ht);


int hash_table_put2( HashTable* ht, unsigned short key, void* value, void (*free_value)( void* ) );

/* get a value indexed by key, return NULL if not found. */
void* hash_table_get(HashTable* ht, unsigned short key);

/* remove a value indexed by key */
void hash_table_rm(HashTable* ht, unsigned short key);

int hash_table_traversal(HashTable* ht,void (*callback)( void* ));
/*
add or update a value to ht,
free_value(if not NULL) is called automatically when the value is removed.
return 0 if success, -1 if error occurred.
*/
#define hash_table_put(ht,key,value) hash_table_put2(ht,key,value,NULL);


#ifdef __cplusplus
}
#endif

#endif
